﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using HexagonWeb.BIL;
using HexagonWeb.IBIL;
using HexagonWeb.Models;

namespace HexagonWeb.Controllers
{
    public class AirlineController : Controller
    {
        private InterfaceAirlineService airlineService;

        private InterfaceUserService userService;

        private InterfaceSeatService seatService;

        private InterfaceTicketService ticketService;

         public AirlineController()
        {
             airlineService = new AirlineService();
             userService = new UserService();
             seatService = new SeatService();
             ticketService = new TicketService();
        }

        // GET: Airline
        public ActionResult Today()
        {
             int record;
             return View(airlineService.FindPageList(1, 10, out record,
                 DateTime.Today, 0));
            
        }

        //订票页面
        public ActionResult Book()
        {
            return View();
        }

        //查询
        public ActionResult  BookList(string EscapeDistrict, string ArriveDistrict, 
            Nullable<DateTime> fromDate, Nullable<DateTime> toDate)
        {
            int record;
            return View(airlineService.FindPageList(out record, 1, 10,
                EscapeDistrict, ArriveDistrict, fromDate, toDate, 0));
        }

        //进入选择页面
        public ActionResult Select(int? ID)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("/User/Login"); 
                return View();
            }
            if (ID == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            else{
                var _user= userService.Find(User.Identity.Name);
                if (_user != null)
                {
                    ViewBag.Option = _user.Option;
                    ViewBag.Service = _user.Service;
                }
                return View(airlineService.Find((int)ID));
            }
        }

        //购买
        public ActionResult Buy(int ID, decimal LevelCost)
        {
            var _airline = airlineService.Find(ID);
            int level = 0 ;
            if (_airline != null)
            {
                if (LevelCost == _airline.LowCost)
                    level = 0;
                else if (LevelCost == _airline.MidCost)
                    level = 1;
                else
                    level = 2;
            }
            else
            {
                ModelState.AddModelError("", "购买失败");
                return View("Select");
            }
            SeatViewModel seatViewModel;
            var _seat = seatService.Find(ID);
            if (_seat != null)
            {
                if (_airline.Type == 0)
                {
                    //经济等级
                    if (level == 0)
                    {
                        if (_seat.LowNum == 280)
                        {
                            ModelState.AddModelError("", "票已经卖光");
                            return View("Select",_airline);
                        }
                        seatViewModel = new SeatViewModel()
                        {
                            AirlineID = ID,
                            Level = 0,
                            Row = (_seat.LowNum + 1) % 8 == 0 ? (_seat.LowNum + 1) / 8 + 10 : (_seat.LowNum + 1) / 8 + 11,
                            Column = (_seat.LowNum + 1) % 8 == 0 ? 8 : (_seat.LowNum + 1) % 8,
                            Type = _airline.Type,
                            Cost = _airline.LowCost
                        };
                        return View(seatViewModel);
                    }
                    //商业等级
                    if (level == 1)
                    {
                        if (_seat.MidNum == 42)
                        {
                            ModelState.AddModelError("", "票已经卖光");
                            return View("Select",_airline);
                        }
                        seatViewModel = new SeatViewModel()
                        {
                            AirlineID = ID,
                            Level = 1,
                            Row = (_seat.MidNum + 1) % 6 == 0 ? (_seat.MidNum + 1) / 6 + 3 : (_seat.MidNum + 1) / 6 + 4,
                            Column = (_seat.MidNum + 1) % 6 == 0 ? 6 : (_seat.MidNum + 1) % 6,
                            Type =_airline.Type,
                            Cost = _airline.MidCost
                        };
                        return View(seatViewModel);
                    }
                    //最高等级
                    if (level == 2)
                    {
                        if (_seat.TopNum == 18)
                        {
                            ModelState.AddModelError("", "票已经卖光");
                            return View("Select",_airline);
                        }
                        
                        seatViewModel = new SeatViewModel()
                        {
                            AirlineID = ID,
                            Level = 2,
                            Row =( (_seat.TopNum + 1) % 6 == 0 ? (_seat.TopNum + 1) / 6 : (_seat.TopNum + 1) / 6 + 1),
                            Column =( (_seat.TopNum + 1) % 6 == 0 ? 6 : (_seat.TopNum + 1) % 6),
                            Type = _airline.Type,
                            Cost = _airline.TopCost
                        };
                        return View(seatViewModel);
                    }
                }
                else
                {
                    //经济等级
                    if (level == 0)
                    {
                        if (_seat.LowNum == 300)
                        {
                            ModelState.AddModelError("", "票已经卖光");
                            return View("Select", _airline);
                        }
                        seatViewModel = new SeatViewModel()
                        {
                            AirlineID = ID,
                            Level = 0,
                            Row = (_seat.LowNum + 1) % 10 == 0 ? (_seat.LowNum + 1) / 10 + 6 : (_seat.LowNum + 1) / 10 + 7,
                            Column = (_seat.LowNum + 1) % 10 == 0 ? 10 : (_seat.LowNum + 1) % 10,
                            Type = _airline.Type,
                            Cost = _airline.LowCost
                        };
                        return View(seatViewModel);
                    }
                    //商业等级
                    if (level == 1)
                    {
                        if (_seat.MidNum == 28)
                        {
                            ModelState.AddModelError("", "票已经卖光");
                            return View("Select",_airline);
                        }
                        seatViewModel = new SeatViewModel()
                        {
                            AirlineID = ID,
                            Level = 1,
                            Row = (_seat.MidNum + 1) %7 == 0 ? (_seat.MidNum + 1) / 7 + 2 : (_seat.MidNum + 1) / 7 + 3,
                            Column = (_seat.MidNum + 1) % 7 == 0 ? 7 : (_seat.MidNum + 1) % 7,
                            Type = _airline.Type,
                            Cost = _airline.MidCost
                        };
                        return View(seatViewModel);
                    }
                    //最高等级
                    if (level == 2)
                    {
                        if (_seat.TopNum == 12)
                        {
                            ModelState.AddModelError("", "票已经卖光");
                            return View("Select",_airline);
                        }
                        seatViewModel = new SeatViewModel()
                        {
                            AirlineID = ID,
                            Level = 2,
                            Row = (_seat.TopNum + 1) % 6 == 0 ? (_seat.TopNum + 1) / 6 : (_seat.TopNum + 1) / 6 + 1,
                            Column = (_seat.TopNum + 1) % 6 == 0 ? 6 : (_seat.TopNum + 1) % 6,
                            Type = _airline.Type,
                            Cost = _airline.TopCost
                        };
                        return View(seatViewModel);
                    }
                }
            }
            ModelState.AddModelError("", "购买失败");
            return View("Select",_airline);
        }
    
        //购买操作
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Buyed(SeatViewModel seatViewModel)
        {
            string PNR = Guid.NewGuid().ToString();
            var _user = userService.Find(User.Identity.Name);
            Ticket ticket = new Ticket()
            {
                PNR = PNR,
                Row = seatViewModel.Row,
                Column = seatViewModel.Column,
                Level = seatViewModel.Level,
                AirlineID = seatViewModel.AirlineID,
                UserID = _user.ID
            };
            var _ticket = ticketService.Add(ticket);
            if (_ticket.UserID > 0)
            {
                var _seat = seatService.Find(seatViewModel.AirlineID);
                if (seatViewModel.Level == 0)
                {
                    _seat.LowNum += 1;
                }
                if (seatViewModel.Level == 1)
                {
                    _seat.MidNum += 1;
                }
                if (seatViewModel.Level == 2)
                {
                    _seat.TopNum += 1;
                }
                seatService.Update(_seat);
                return View();
            }
            ModelState.AddModelError("", "购买失败");
            return View();
        }
    }
}